package com.docios.cookbook2;

import android.content.Context;
import android.util.Log;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

import com.docios.cookbook2.entity.Category;
import com.docios.cookbook2.entity.MaterialBean;
import com.docios.cookbook2.entity.ProcessBean;
import com.docios.cookbook2.entity.QueryBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("com.docios.cookbook2", appContext.getPackageName());
    }


    @Test
    public void jsonByClass() {
        // 获取应用的Context
        Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

        try {
            // 获取json文件的输入流 byClass
            InputStream inputStream = context.getAssets().open("byType.json");
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            inputStream.close();
            //得到json字符串
            String jsonString = new String(buffer, StandardCharsets.UTF_8);
            /*
             * TODO 到此都是ok的
             * */


            Gson gson = new Gson();
            JSONArray listArray = new JSONObject(jsonString).getJSONObject("result").getJSONArray("list");

            /*1层
             * 获取queryBean数组
             * queryBeans里没有material和process的数据！
             * */
            List<QueryBean> queryBeans = gson.fromJson(listArray.toString(), new TypeToken<List<QueryBean>>() {
            }.getType());

            /*2层
             * 获取 (material数组 和 process数组) 的数组
             * */
            for (int i = 0; i < listArray.length(); i++) {

                JSONObject listObject = listArray.getJSONObject(i);
                JSONArray materialArray = listObject.getJSONArray("material");
                JSONArray processArray = listObject.getJSONArray("process");

                List<MaterialBean> materialBeans = gson.fromJson(materialArray.toString(), new TypeToken<List<MaterialBean>>() {
                }.getType());
                List<ProcessBean> processBeans = gson.fromJson(processArray.toString(), new TypeToken<List<ProcessBean>>() {
                }.getType());

                //存入两数组
                queryBeans.get(i).setMaterialBeans(materialBeans);
                queryBeans.get(i).setProcessBeans(processBeans);
            }

            Log.i("mytag", "json: " + queryBeans.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Test
    public void jsonById() {
        // 获取应用的Context
        Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

        try {
            // 获取json文件的输入流 byClass
            InputStream inputStream = context.getAssets().open("byId.json");
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            inputStream.close();
            //得到json字符串
            String jsonString = new String(buffer, StandardCharsets.UTF_8);
            /*
             * 模拟得到响应数据
             * */


            Gson gson = new Gson();
            JSONObject listObject = new JSONObject(jsonString).getJSONObject("result");

            /*1层
             * 获取queryBean数组
             * queryBeans里没有material和process的数据！
             * */
            QueryBean queryBean = gson.fromJson(listObject.toString(), new TypeToken<QueryBean>() {
            }.getType());

            /*2层
             * 获取 (material数组 和 process数组) 的数组
             * */

            JSONArray materialArray = listObject.getJSONArray("material");
            JSONArray processArray = listObject.getJSONArray("process");

            List<MaterialBean> materialBeans = gson.fromJson(materialArray.toString(), new TypeToken<List<MaterialBean>>() {
            }.getType());
            List<ProcessBean> processBeans = gson.fromJson(processArray.toString(), new TypeToken<List<ProcessBean>>() {
            }.getType());

            //存入两数组
            queryBean.setMaterialBeans(materialBeans);
            queryBean.setProcessBeans(processBeans);


            Log.i("mytag", "json: " + queryBean);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Test
    public void jsonTypes() {
        // 获取应用的Context
        Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

        try {
            // 获取json文件的输入流 byClass
            InputStream inputStream = context.getAssets().open("type.json");
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            inputStream.close();
            //得到json字符串
            String jsonString = new String(buffer, StandardCharsets.UTF_8);
            /*
             * TODO 到此都是ok的
             * */


            Gson gson = new Gson();
            JSONArray resultArray = new JSONObject(jsonString).getJSONArray("result");
            /*1层
             * 获取一级分类：功效，人群，疾病。。。
             * */
            List<Category> categories = gson.fromJson(resultArray.toString(), new TypeToken<List<Category>>() {
            }.getType());

            /*2层
             * 获取 二级分类：减肥，美容，排毒。。。
             * */
            for (int i = 0; i < resultArray.length(); i++) {

                JSONObject CListObject = resultArray.getJSONObject(i);
                JSONArray CListArray = CListObject.getJSONArray("list");

                List<Category.CList> CLists = gson.fromJson(CListArray.toString(), new TypeToken<List<Category.CList>>() {
                }.getType());

                //存入数组
                categories.get(i).setCLists(CLists);
            }

            System.out.println("################################");
            System.out.println(categories);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}